package edu.ciee.app.jichuxinxi.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import edu.ciee.app.action.util.JdbcUtil;
import edu.ciee.app.action.util.SessionUtil;

public class ZhiliaoshouduanAction {

	/*
	 * ID int auto_increment, zhiliao_desc varchar(50), yaowu int,
	 */

	private int id;
	private String zhiliao_desc;
	private int yaowu;
	private String tip;
	private List resultList;

	public List getResultList() {
		return resultList;
	}

	public void setResultList(List resultList) {
		this.resultList = resultList;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getZhiliao_desc() {
		return zhiliao_desc;
	}

	public void setZhiliao_desc(String zhiliao_desc) {
		this.zhiliao_desc = zhiliao_desc;
	}

	public int getYaowu() {
		return yaowu;
	}

	public void setYaowu(int yaowu) {
		this.yaowu = yaowu;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public String insert() throws Exception {

		// System.out.println("this.getProduct_type()=" +
		// this.getProduct_type());

		String sql = "insert into zhiliao_table(zhiliao_desc,yaowu) values("
			+ "\'" + this.getZhiliao_desc() + "\'," + "\'"
			+ this.getYaowu() + "\');";

		System.out.println(sql);
		try {
			JdbcUtil.updateResult(sql,"yzsystem");
			ActionContext.getContext().getSession().remove("zhiliaomap");
			SessionUtil.updateSession("zhiliao_table", "zhiliao_desc","zhiliaomap","yzsystem");
			return "refresh";
		} catch (Exception e) {
			return "error";
		}
	}

	public String search() {

		String query = "where 1=1";

		if (this.getId() != 0) {
			query = query + " and ID like '%" + this.getId() + "%'";// this.getID()
		}
		if (this.getZhiliao_desc() != null) {
			query = query + " and zhiliao_desc like '%"
					+ this.getZhiliao_desc() + "%'"; // this.getName()
		}
		if (this.getYaowu() != 0) {
			query = query + " and yaowu like '%" + this.getYaowu() + "%'"; // this.getName()
		}
		String sql = "select zhiliao_table.ID as zhiliao_id,zhiliao_desc,zhiliao_table.yaowu as zhiliao_yaowu,yaowu_table.name as yaowu_name " +
		" from zhiliao_table,yaowu_table " + query+" and zhiliao_table.yaowu=yaowu_table.ID";
		//String sql = "select * from zhiliao_table " + query;
		System.out.println(sql);
		try {
			resultList = JdbcUtil.searchResult(sql,"yzsystem");
			return "success";
		} catch (Exception e) {
			return "error";
		}
	}

	public String delete() {

		HttpServletRequest request = ServletActionContext.getRequest();
		ActionContext ctx = ActionContext.getContext();
		Map m = ctx.getParameters();
		String id = request.getParameter("id");
		System.out.println(id);
		String sql = "delete from zhiliao_table where ID=" + id + ";";
		System.out.println(sql);
		try {
			JdbcUtil.updateResult(sql,"yzsystem");
			ActionContext.getContext().getSession().remove("zhiliaomap");
			SessionUtil.updateSession("zhiliao_table", "zhiliao_desc", "zhiliaomap","yzsystem");
			return "refresh";
		} catch (Exception e) {
			this.setTip("error" + e.toString());
			return "error";
		}

	}

	public String forEdit() {
//		HttpServletRequest request = ServletActionContext.getRequest();
//		ActionContext ctx = ActionContext.getContext();
//		Map m = ctx.getParameters();
//		String zhiliao_desc = request.getParameter("zhiliao_desc");
//		int id = Integer.parseInt(request.getParameter("id"));
//		int yaowu = Integer.parseInt(request.getParameter("yaowu"));
		System.out.println(zhiliao_desc);
		System.out.println("id=" + id);
		System.out.println("yaowu=" + yaowu);
//		this.setZhiliao_desc(zhiliao_desc);
//		this.setId(id);
//		this.setYaowu(yaowu);

		return "success";
	}

	public String edit() {

		String sql = "update zhiliao_table set zhiliao_desc = \'" + this.getZhiliao_desc()
				+ "\',yaowu=\'" + this.getYaowu() 
				+ "\' where ID=" + this.getId() + ";";
		System.out.println(sql);
		try {
			JdbcUtil.updateResult(sql,"yzsystem");
			ActionContext.getContext().getSession().remove("zhiliaomap");
			SessionUtil.updateSession("zhiliao_table", "zhiliao_desc","zhiliaomap","yzsystem");
			return "refresh";
		} catch (Exception e) {
			this.setTip(e.toString());
			return "error";
		}
	}

}
